package com.nirvana.admin.controller;

import com.nirvana.admin.entity.vo.AuthInfoResponseVO;
import com.nirvana.admin.entity.vo.AuthMenuResponseVO;
import com.nirvana.admin.entity.vo.AuthRequestVO;
import com.nirvana.admin.entity.vo.AuthResponseVO;
import com.nirvana.admin.service.AuthService;
import com.nirvana.common.constant.BaseMessage;
import com.nirvana.common.dto.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.nirvana.common.util.ServletUtils.getClientIp;
import static com.nirvana.common.util.ServletUtils.getUserAgent;

/**
 * @author zhangshuaiyin
 * @date 2021-09-29 14:27
 */
@Api(tags = "认证接口")
@RestController
@RequestMapping
@Validated
public class AuthController {

    @Resource
    private AuthService authService;

    @PostMapping("/login")
    @ApiOperation("使用账号密码登录")
    public BaseResponse<AuthResponseVO> login(@RequestBody @Valid AuthRequestVO requestVO) {
        String token = authService.login(requestVO, getClientIp(), getUserAgent());
        // 返回结果
        return BaseMessage.SUCCESS.response(AuthResponseVO.builder().token(token).build());
    }

    @GetMapping("/permissions")
    @ApiOperation("获取登陆用户的权限信息")
    public BaseResponse<AuthInfoResponseVO> permissions() {
        AuthInfoResponseVO authInfoResponseVO = authService.getPermissionInfo();
        // 返回结果
        return BaseMessage.SUCCESS.response(authInfoResponseVO);
    }

    @GetMapping("/routers")
    @ApiOperation("获得登陆用户的菜单路由列表")
    public BaseResponse<List<AuthMenuResponseVO>> getMenus() {
        // 获得用户拥有的菜单树
        List<AuthMenuResponseVO> menuList = authService.getRouters();
        return BaseMessage.SUCCESS.response(menuList);
    }
}
